<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }
        .container {
            width: 90%;
            max-width: 400px;
            background-color: #fff;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            text-align: center;
        }
        .input-field {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
        }
        .login-button, .register-button {
            width: 100%;
            padding: 15px;
            margin: 10px 0;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
        }
        .login-button:hover, .register-button:hover {
            background-color: #0056b3;
        }
        #loginMessage {
            margin-top: 10px;
            font-size: 14px;
        }
        .register-button {
            background-color: #28a745;
        }
        .register-button:hover {
            background-color: #218838;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>用户登录</h1>
        <form id="loginForm">
            <input type="text" id="username" class="input-field" placeholder="用户名或姓名" required>
            <input type="password" id="password" class="input-field" placeholder="密码" required>
            <button type="submit" class="login-button">登录</button>
        </form>
        <button class="register-button" onclick="window.location.href='注册.html'">注册</button>
        <p id="loginMessage"></p>
    </div>

    <script>
        async function checkLoginStatus() {
            try {
                const response = await fetch('http://localhost:3000/user-info', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    credentials: 'include' // 确保发送 cookie
                });

                if (!response.ok) {
                    // 如果未登录，跳转到登录页面
                    window.location.href = '登录.html';
                }
            } catch (error) {
                console.error('检查登录状态失败:', error);
                window.location.href = '登录.html'; // 请求失败时也跳转到登录页面
            }
        }

        const loginForm = document.getElementById('loginForm');
        const loginMessage = document.getElementById('loginMessage');

        loginForm.addEventListener('submit', async (e) => {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            try {
                const response = await fetch('http://localhost:3000/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    credentials: 'include', // 确保发送和接收 Cookie
                    body: JSON.stringify({ username, password }),
                });

                if (response.ok) {
                    loginMessage.textContent = '登录成功，正在跳转...';
                    loginMessage.style.color = 'green';
                    setTimeout(() => {
                        window.location.href = '主页.html'; // 替换为实际的主页路径
                    }, 1000);
                } else {
                    const errorText = await response.text();
                    loginMessage.textContent = errorText;
                    loginMessage.style.color = 'red';
                }
            } catch (error) {
                console.error('登录请求失败:', error);
                loginMessage.textContent = '服务器错误，请稍后重试';
                loginMessage.style.color = 'red';
            }
        });
    </script>
</body>
</html>